/**
 * https://leetcode-cn.com/problems/number-of-1-bits/
 * 位为1的个数
 * @param n 
 */
const hammingWeight = (n: number): number => {
    let ans = 0;
    while (n) {
        // 刚好能消掉一个1
        n &= (n - 1)
        ans++
    }
    return ans
};



const hammingWeight3 = (n: number): number => {
    let ans = 0;
    for (let i = 0; i < 32; i++) {
        // 该位为1
        if ((n & (1 << i)) === 1) {
            ans++
        }
    }
    return ans
};


const hammingWeight2 = (n: number): number => {
    // 将二进制转为字符串
    const s = n.toString(2).toString();

    let len = s.length - 1,
        ans = 0;

    for (let i = len; i >= 0; i--) {
        if (s[i] === '1') ans++
    }

    return ans
};